﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;

namespace Clinic.Control.Controls
{
    public class CheckItem : System.Windows.Controls.ListBoxItem, ICommandSource
    {
        // 定义一个依赖属性来存储命令
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(CheckItem), new PropertyMetadata(null));

        // 定义一个依赖属性来存储命令参数
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(CheckItem), new PropertyMetadata(null));

        // 定义一个依赖属性来存储命令目标
        public static readonly DependencyProperty CommandTargetProperty =
            DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(CheckItem), new PropertyMetadata(null));

        // 属性包装器
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        public object CommandParameter
        {
            get { return GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }

        public IInputElement CommandTarget
        {
            get { return (IInputElement)GetValue(CommandTargetProperty); }
            set { SetValue(CommandTargetProperty, value); }
        }

        // 构造函数
        static CheckItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckItem), new FrameworkPropertyMetadata(typeof(CheckItem)));
        }

        // 重写 OnMouseDown 方法以触发命令
        protected override void OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            if (Command != null && Command.CanExecute(CommandParameter))
            {
                Command.Execute(CommandParameter);
            }
            if (!IsSelected)
                IsSelected = true;
        }
    }

}
